#include <stdio.h>

#include "globals.h"
#include "arm7tdmi.h"
#include "arm_mode.h"

void init_processor()
{
	int i;

	/* clear general purpose registers (maybe not needed) */
	for(i = 0; i < 16; i++)
		r[i] = 0;

    /* initialize PC, 2 words ahead */
    PC = 0x8;

	/* initialize CPSR */
	CPSR = 0x00000010;  /* user mode */

	/* initialize processor state */
	proc_state = NORMAL_STATE;
}

void load_program(FILE *obj_code)
{
	int i = 0;
	int c;
	while((c = getchar()) != EOF)
	{
		ram[i] = c;
		i++;
	}
}

void execute(FILE *obj_code)
{
	u32 instruction;
	u32 cond;
	u32 ident;

	init_processor();
	load_program(obj_code);

	while(1)
	{
	    /* sincronization conditions, maybe .... */

        if(THUMB_MODE)
        {
            fetch_and_execute_thumb();
        }
        else
        {
            fetch_and_execute_arm();
        }

		/* verify halt conditions */
		if(proc_state != NORMAL_STATE)
            return;  /* in the future, print error and register dump */
	}
}
